package com.mall.mapper;

import com.mall.pojo.vo.CartItemVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 购物车Mapper接口
 */
@Mapper
public interface CartMapper {
    
    /**
     * 根据用户ID查询购物车中选中的商品
     */
    @Select("SELECT g.id, g.name, g.price, g.image, g.description, g.oldPrice, c.count, c.selected, c.attrs_text AS attrsText " +
            "FROM goods g INNER JOIN cart c ON g.id = c.goods_id " +
            "WHERE c.user_id = #{userId} AND c.selected = 1")
    List<CartItemVO> selectCheckedByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID查询购物车所有商品
     */
    @Select("SELECT g.id, g.name, g.price, g.image, g.description, g.oldPrice, c.count, c.selected, c.attrs_text AS attrsText " +
            "FROM goods g INNER JOIN cart c ON g.id = c.goods_id " +
            "WHERE c.user_id = #{userId}")
    List<CartItemVO> selectAllByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID和商品ID删除购物车中对应的商品
     */
    @Delete("DELETE FROM cart WHERE user_id = #{userId} AND goods_id = #{goodsId}")
    int deleteCartItem(@Param("userId") Long userId, @Param("goodsId") Long goodsId);
    
    /**
     * 根据用户ID清空购物车中已选择的商品
     */
    @Delete("DELETE FROM cart WHERE user_id = #{userId} AND selected = 1")
    int deleteCheckedByUserId(@Param("userId") Long userId);
} 